package com.dy.mongo.controller;

import com.dy.mongo.dto.BatchResult;
import com.dy.mongo.service.BatchMediaImportService;
import com.dy.mongo.service.MediaService;
import com.opencsv.exceptions.CsvValidationException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import org.springframework.http.MediaType;

/**
 * 媒体上传相关接口。
 */
@RestController
@RequestMapping("/api/media")
@RequiredArgsConstructor
public class MediaController {

    private final BatchMediaImportService batchService;
    private final MediaService mediaService;

    /**
     * 批量上传 Zip 媒体包（含 manifest.csv）。
     */
    @PostMapping(value = "/batch", consumes = "multipart/form-data")
    public ResponseEntity<?> uploadBatch(@RequestPart("zipFile") MultipartFile zipFile) throws IOException, CsvValidationException {
        if (zipFile.isEmpty()) {
            return ResponseEntity.badRequest().body("zipFile 不能为空");
        }
        BatchResult result = batchService.importZip(zipFile);
        return ResponseEntity.ok(result);
    }

    /**
     * 单文件上传到指定人物
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<?> uploadSingle(@RequestPart("file") MultipartFile file,
                                          @RequestParam Long csvId,
                                          @RequestParam String type,
                                          @RequestParam String title,
                                          @RequestParam(required = false) String description) {
        try {
            mediaService.uploadSingle(csvId, type, title, description, file);
            return ResponseEntity.ok("上传成功");
        } catch (IllegalArgumentException | IOException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
} 